<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright 2011 KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: participant.tpl 2011-03-09 01:11:30 idris $
 * @author     Idris
 */
?>
  
<?php if ($this->isPart): ?>

<div class="vote-participant-badge">

  <div class="left">
    <div class="photo">
      <?php echo $this->itemPhoto($this->participant, 'thumb.profile'); ?>
    </div>
  </div>
  
  <div class="right">
    <div class="info">
      <div class="item_title">
        <?php echo $this->htmlLink($this->participant->getHref(), $this->participant->getTitle()); ?>
      </div>
      <div class="rate">
        <?php echo $this->objectRate($this->participant, $this->canVote); ?>
      </div>
      <div class="item_description">
        <?php echo $this->participant->getDescription(); ?>
      </div>
      <?php if ($this->participant->status != 1): ?>
      <div class="tip">
        <span><?php echo $this->translate('Your video is now processing encoding.'); ?></span>
      </div>
      <?php endif; ?>
      <div class="item_details">
        <ul class="vote_browse_info_misc">
          <?php if ($this->participant->view_count > 0): ?>
            <li class="views">
              <span class="label"><?php echo $this->translate('Views'); ?></span>
              <span class="value"><?php echo $this->translate(array('%s view', '%s views', $this->participant->view_count), $this->participant->view_count); ?></span>
              <div class="clr"></div>
            </li>
          <?php endif; ?>
          <?php if ($this->participant->vote_count > 0): ?>
            <li class="votes">
              <span class="label"><?php echo $this->translate('Votes'); ?></span>
              <span class="value"><?php echo $this->ratersFluentList($this->participant); ?></span>
              <div class="clr"></div>
            </li>
          <?php endif; ?>
          <?php if ($this->participant->comment_count > 0): ?>
            <li class="comments">
              <span class="label"><?php echo $this->translate('Comments'); ?></span>
              <span class="value"><?php echo $this->translate(array('%s comment', '%s comments', $item->comment_count), $this->participant->comment_count); ?></span>
              <div class="clr"></div>
            </li>
          <?php endif; ?>
        </ul>
      </div>
      <?php if (!$this->subject->isFinished()): ?>
      <div class="item_options">
        <?php echo $this->htmlLink($this->url(array('controller' => 'vote', 'action' => 'disqualify', 'format' => 'smoothbox', 'id' => $this->participant->getIdentity()), 'vote_general'), $this->translate('Disqualification'), array('class' => 'smoothbox delete')); ?>
      </div>
      <?php endif; ?>
    </div>
  </div>

  <div class="clr"></div>
</div>

<?php else: ?>

<script type="text/javascript">
var current_code;

var ignoreValidation = function()
{
  $('upload-wrapper').style.display = "block";
  $('validation').style.display = "none";
  $('code').value = current_code;
  $('ignore').value = true;
}
  
var updateTextFields = function()
{
  var video_element = document.getElementById("video_type");
  var url_element = document.getElementById("url-wrapper");
  var file_element = document.getElementById("file-wrapper");
  var submit_element = document.getElementById("upload-wrapper");

  // clear url if input field on change
  // $('code').value = "";
  $('upload-wrapper').style.display = "none";

  // If video source is empty
  if (video_element.value == 0)
  {
    $('url').value = "";
    file_element.style.display = "none";
    url_element.style.display = "none";
    return;
  }

  if ($('code').value && $('url').value)
  {
    $('video_type-wrapper').style.display = "none";
    file_element.style.display = "none";
    $('upload-wrapper').style.display = "block";
    return;
  }

  // If video source is youtube or vimeo
  if (video_element.value == 1 || video_element.value == 2)
  {
    $('url').value = "";
    $('code').value = "";
    file_element.style.display = "none";
    url_element.style.display = "block";
    return;
  }

  // If video source is from computer
  if (video_element.value == 3)
  {
    $('url').value = "";
    $('code').value = "";
    file_element.style.display = "block";
    url_element.style.display = "none";
    return;
  }

  // if there is video_id that means this form is returned from uploading because some other required field
  if ($('id').value)
  {
    $('video_type-wrapper').style.display = "none";
    file_element.style.display = "none";
    $('upload-wrapper').style.display = "block";
    return;
  }
}


var video = {

    active : false,

    debug : false,

    currentUrl : null,

    currentTitle : null,

    currentDescription : null,

    currentImage : 0,

    currentImageSrc : null,

    imagesLoading : 0,

    images : [],

    maxAspect : (10 / 3), //(5 / 2), //3.1,

    minAspect : (3 / 10), //(2 / 5), //(1 / 3.1),

    minSize : 50,

    maxPixels : 500000,

    monitorInterval: null,

    monitorLastActivity : false,

    monitorDelay : 500,

    maxImageLoading : 5000,

    attach : function()
    {
      var bind = this;
      $('url').addEvent('keyup', function()
      {
        bind.monitorLastActivity = (new Date).valueOf();
      });

      var url_element = document.getElementById("url-element");
      var myElement = new Element("p");
      myElement.innerHTML = "test";
      myElement.addClass("description");
      myElement.id = "validation";
      myElement.style.display = "none";
      url_element.appendChild(myElement);

      var body = $('url');
      var lastBody = '';
      var lastMatch = '';
      var video_element = $('video_type');
      (function()
      {
        if( body.value == lastBody || bind.currentUrl )
        {
          return;
        }

        // Ignore if delay not met yet
        if( (new Date).valueOf() < bind.monitorLastActivity + bind.monitorDelay )
        {
          return;
        }

        // Check for link
        var m = body.value.match(/http:\/\/([-\w\.]+)+(:\d+)?(\/([-#:\w/_\.]*(\?\S+)?)?)?/);
        if( $type(m) && $type(m[0]) && lastMatch != m[0] )
        {
          if (video_element.value == 1){
            video.youtube(body.value);
          }
          else video.vimeo(body.value);
        }
        else{

        }

        lastBody = body.value;
      }).periodical(250);
    },

    youtube : function(url) {
      var myURI = new URI(url);
      var youtube_code = myURI.get('data')['v'];

      if (youtube_code){
        (new Request.HTML({
          'format': 'html',
          'url' : '<?php echo $this->url(array('controller' => 'video', 'action' => 'validation'), 'vote_general', true) ?>',
          'data' : {
            'ajax' : true,
            'code' : youtube_code,
            'type' : 'youtube',
            'challenge_id' : <?php echo (int)$this->subject->getIdentity(); ?>
          },
          'onRequest' : function(){
            $('validation').style.display = "block";
            $('validation').innerHTML = '<?php echo $this->string()->escapeJavascript($this->translate('Checking URL...'));?>';
            $('upload-wrapper').style.display = "none";
          },
          'onSuccess' : function(responseTree, responseElements, responseHTML, responseJavaScript)
          {
            if (valid){
              $('upload-wrapper').style.display = "block";
              $('validation').style.display = "none";
              $('code').value = youtube_code;
            }
            else{
              $('upload-wrapper').style.display = "none";
              current_code = youtube_code;
              <?php $link = "<a href='javascript:void(0);' onclick='javascript:ignoreValidation();'>".$this->translate("here").'</a>'; ?>
                    $('validation').innerHTML = '<?php echo addslashes($this->translate("We could not find a video there - please check the URL and try again. If you are sure that the URL is valid, please click %s to continue.", $link)); ?>';
            }
          }
        })).send();
      }
    },

    vimeo : function(url) {
      var myURI = new URI(url);
      var vimeo_code = myURI.get('file');
      if (vimeo_code.length > 0){
        (new Request.HTML({
          'format': 'html',
          'url' : '<?php echo $this->url(array('controller' => 'video', 'action' => 'validation'), 'vote_general', true) ?>',
          'data' : {
            'ajax' : true,
            'code' : vimeo_code,
            'type' : 'vimeo',
            'challenge_id' : <?php echo (int)$this->subject->getIdentity(); ?>
          },
          'onRequest' : function(){
            $('validation').style.display = "block";
            $('validation').innerHTML = '<?php echo $this->string()->escapeJavascript($this->translate('Checking URL...'));?>';
            $('upload-wrapper').style.display = "none";
          },
          'onSuccess' : function(responseTree, responseElements, responseHTML, responseJavaScript)
          {
            if (valid){
              $('upload-wrapper').style.display = "block";
              $('validation').style.display = "none";
              $('code').value = vimeo_code;
            }
            else{
              $('upload-wrapper').style.display = "none";
              current_code = vimeo_code;
              <?php $link = "<a href='javascript:void(0);' onclick='javascript:ignoreValidation();'>".$this->translate("here")."</a>"; ?>
              $('validation').innerHTML = "<?php echo $this->translate("We could not find a video there - please check the URL and try again. If you are sure that the URL is valid, please click %s to continue.", $link); ?>";
            }
          }
        })).send();
      }
    }
}

en4.core.runonce.add(function(){
  updateTextFields();
  video.attach();
});
</script>

<div class="vote-participant-form">
  <?php echo $this->form->render($this); ?>
</div>

<?php endif; ?>